﻿using System;
using System.Collections.Generic;
using System.Threading;
using DM4;
using sscore;

namespace DMN_MailSender
{
    public delegate void WorkerResultHandler(WorkerResult wr);
    internal delegate void SpamerResultHandler(TaskParams tprm);

    public delegate OperationResult LogHandler(string aMessage, MessageType type = MessageType.Info);
    public delegate void  MailSendHandler(List<MailSenderWorkerParameters> mailParams);

    public class WorkerResult
    {
        public int CompleteCount;
        public int ErrorCount;
    }

    internal class TaskParams
    {
        public CancellationToken Ct;
        public LogHandler Log;
        public Spamer Spamer;
        public MailSenderWorkerParameters MailParams;
        public string ConnectionString;
    }

    internal class CreateMailParamsTaskParams
    {
        public CancellationToken Ct;
        public List<MailSenderParameters> Mswp;
        public MailSendHandler MailSend;
    }

    public class MailSenderWorkerParameters
    {
        public DateTime StartTime = DateTime.Now;
        public MailAccount AccFrom;
        public string AccTo;
        public MailSendTemplate MailTemplate;
        public Dictionary<string, string> Macros = new Dictionary<string, string>();
        public string SubjectText = "";
        public string BodyText = "";
        public int Timeout = MailSenderConsts.Timeout;
        public int AttemptsTotal = 1;
        public int AttemptsPerformed;
        public string Result = Consts.FALSE;
        public string ResultMessage = "";

        public MailSenderWorkerParameters(MailAccount accountFrom, string mailTo, MailSendTemplate mailMailTemplate, Dictionary<string, string> macros, int attempts = 1, int timeout = MailSenderConsts.Timeout)
        {
            AccFrom = accountFrom;
            MailTemplate = mailMailTemplate;
            Macros = macros;
            AttemptsTotal = attempts;
            AccTo = mailTo;
            ApplyMacros(macros);

            Timeout = timeout;
        }

        public MailSenderWorkerParameters(DateTime startTime, MailAccount accountFrom, string mailTo, MailSendTemplate mailMailTemplate
            , string subjectText, string bodyText, int attemptsTotal, int attemptsPerformed, int timeout = MailSenderConsts.Timeout)
        {
            StartTime = startTime;

            AccFrom = accountFrom;
            AccTo = mailTo;
            MailTemplate = mailMailTemplate;

            SubjectText = subjectText;
            BodyText = bodyText;

            AttemptsTotal = attemptsTotal;
            AttemptsPerformed = attemptsPerformed;

            Timeout = timeout;
        }

        public void ApplyMacros(Dictionary<string, string> macros)
        {
            Macros = macros;
            SubjectText = MailTemplate.Subject;
            BodyText = MailTemplate.Body;

            if (Macros != null && Macros.Count > 0)
            {
                if (!String.IsNullOrEmpty(SubjectText))
                    foreach (KeyValuePair<string, string> kvp in Macros)
                        if (kvp.Key != null && kvp.Value != null)
                            SubjectText = SubjectText.Replace(kvp.Key, kvp.Value);

                if (!String.IsNullOrEmpty(BodyText))
                    foreach (KeyValuePair<string, string> kvp in Macros)
                        if (kvp.Key != null && kvp.Value != null)
                            BodyText = BodyText.Replace(kvp.Key, kvp.Value);
            }
        }

    }
}
